import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


import login from "../pages/login/login.vue"
import index from "../pages/index/index.vue"
import search from "../pages/search/search.vue"
import detail from "../pages/detail/detail.vue"
import confirm from "../pages/confirm/confirm.vue"
import address from "../pages/address/address.vue"
import addaddress from "../pages/addaddress/addaddress.vue"
import manage from "../pages/manage/manage.vue"
import place from "../pages/place/place.vue"
import pay from "../pages/pay/pay.vue"
import register from "../pages/register/register.vue"


import home from "../pages/home/home.vue"
import list from "../pages/list/list.vue"
import shop from "../pages/shop/shop.vue"
import mine from "../pages/mine/mine.vue"
import cate from "../pages/cate/cate.vue"

const routes = [
  {
    path:"/login",
    component:login
  },
  {
    path:"/register",
    component:register
  },
  {
    path:"/search",
    component:search
  },
  {
    path:"/list",
    component:list
  },
  {
    path:"/detail/:id",
    component:detail,
    // meta: {
    //   title: "商品详情",
      
    // },
    // // 路由独享守卫
    // beforeEnter(to, from, next) {
    //   console.log("路由独享守卫：beforeEnter");
    //   let type = localStorage.getItem("type")
    //   // 管理员才能看
    //   if (type === "2") {
    //     next()
    //   } else {
    //     next("/index/mine")
    //   }
    // }
  },
  {
    path:"/confirm",
    component:confirm
  },
  {
    path:"/address",
    component:address
  },
  {
    path:"/addaddress",
    component:addaddress
  },
  {
    path:"/manage",
    component:manage
  },
  {
    path:"/place",
    component:place
  },
  
  {
    path:"/pay",
    component:pay
  },
  {
    path:"/index",
    component:index,
    children:[
       {
        path:"home",
        component:home
      },
     
     {
        path:"shop",
        component:shop
      },
      {
        path:"mine",
        component:mine
      },
      {
        path:"cate",
        component:cate
      },
    
      //二级重定向
      {
        path:"",
        redirect:"home"
      }
    ]
  },
 
  //一级重定向
  {
    path:"*",
    redirect:"login"
  }
]

const router = new VueRouter({
  mode:"hash",
  //路由规则
  routes: routes,
  //滚动行为
  scrollBehavior(to, from, savePosition) {
    if (savePosition) {
      return savePosition
    } else {
      return {
        x: 0,
        y: 0
      }
    }

  }
})



//全局守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login" || to.path == "/register") {
    next()
    return;
  };
  let isLogin = localStorage.getItem("isLogin");
  if (isLogin) {
    next()
    return;
  }
  next("/login")
})

router.afterEach((to, from) => {
  console.log("进入完成 afterEach");
})
export default router
